import 'package:flutter_module/login/userInfo.dart';
import 'package:flutter_module/tool/userManager.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'dart:convert';
import 'package:flutter_module/tabbar/shop/deviceModel.dart';

class FileManager  {

  /// 保存用户信息
  static saveUserInfo(Map userInfo) async {

    // 获取应用文档目录
    Directory documentsDir = await getApplicationDocumentsDirectory();
    String documentsPath = documentsDir.path;

    String userId =  userInfo['userId'];
    if (userId == null) return;

    String userInfoPath = documentsPath + '/' + 'userInfo';
    File userInfoFile = File(userInfoPath);
    // 判断文件是否存在
    if(!userInfoFile.existsSync()){
      userInfoFile.createSync();
    }

    userInfoFile.writeAsString(json.encode(userInfo).toString()).then((val){
      print(val);
    });
  }

  /// 获取用户信息
  static Future<UserInfo> getUserInfo() async {
    Directory documentsDir = await getApplicationDocumentsDirectory();
    String documentsPath = documentsDir.path;

    /// 存储文件目录
    String filePath = documentsPath + '/' + 'userInfo';

    File file = File(filePath);
    if(!file.existsSync()){
      return null;
    }
    
    String val = file.readAsStringSync();
    Map<String,dynamic> map = (json.decode(val.toString()) as Map).cast();
    UserInfo userInfo = UserInfo.fromJson(map);
    return userInfo;
  }

  /// 保存设备信息
  static saveDevicesString(List devicesList) async {

    if (UserManager().userInfo.userId == null) {
      print('userId为空,设备信息保存失败');
      return;
    }
    // 获取应用文档目录
    Directory documentsDir = await getApplicationDocumentsDirectory();
    String documentsPath = documentsDir.path;
    String userPath = documentsPath + '/' + UserManager().userInfo.userId;

    File userFile = File(userPath);
    // 判断当前目录路径是否存在
    if (!userFile.existsSync()) {
      /// 不存在则创建子目录创建子目录
      await Directory(userPath).create(recursive: true);
    }

    String devicesPath = userPath + '/' + 'devicesString';
    File devicesFile = File(devicesPath);
    // 判断文件是否存在
    if(!devicesFile.existsSync()){
      devicesFile.createSync();
    }

    devicesFile.writeAsString(json.encode(devicesList).toString()).then((val){
      print('设备信息保存成功');
    }).catchError((error){
      print('设备信息保存失败');
    });

  }

  /// 获取设备信息
  static Future fetchDevices() async {
    if (UserManager().userInfo.userId == null) {
      print('userId为空,设备信息获取失败');
      return;
    }
    // 获取应用文档目录
    Directory documentsDir = await getApplicationDocumentsDirectory();
    String documentsPath = documentsDir.path;
    String userPath = documentsPath + '/' + UserManager().userInfo.userId;

    String devicesPath = userPath + '/' + 'devicesString';
    File devicesFile = File(devicesPath);
    // 判断文件是否存在
    if(!devicesFile.existsSync()){
      print('目录为空,设备信息获取失败');
      return;
    }

    String val = devicesFile.readAsStringSync();
    List<Map> devicesList = (json.decode(val.toString()) as List).cast();

    List<DeviceModel> deviceModels = [];
    devicesList.forEach((val){
      DeviceModel model = DeviceModel.fromJson(val);
      deviceModels.add(model);
    });

    return deviceModels;
  }

}